//
//  IGWebViewController.m
//  jingdongfang
//
//  Created by BOE on 2017/10/25.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGWebViewController.h"
#import <WebKit/WebKit.h>

@interface IGWebViewController ()<WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, assign) IGWebViewType type;

@property (nonatomic, copy) NSString *urlStr;

@end

@implementation IGWebViewController

- (instancetype)initWithType:(IGWebViewType)type
{
    self = [super init];
    if (self) {
        _type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
//    self.urlStr = [NSString stringWithFormat:@"%@agreement%@.html", [[IGServerConfig sharedConfig] httpApiBaseUrl], @(_type)];
    self.urlStr = [NSString stringWithFormat:@"%@sys/agreement%@.html", [[IGServerConfig sharedConfig] httpApiBaseUrl], @(_type)];
}

- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] init];
        _webView.navigationDelegate = self;
        
        _webView.backgroundColor = [UIColor clearColor];
        _webView.allowsBackForwardNavigationGestures = YES;
    }
    return _webView;
}

- (void)setUrlStr:(NSString *)urlStr
{
    if (_urlStr == urlStr) {
        return;
    }
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:KLanguage];
    [request addValue:language forHTTPHeaderField:@"locale"];
    
    [self.webView loadRequest:request];
//    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [webView evaluateJavaScript:@"document.title;" completionHandler:^(id Result, NSError * error) {
        NSString *title = Result;
        self.title = title;
    }];
}

-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
    if (!navigationAction.targetFrame.isMainFrame)
    {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
